package com.caocaog.market.util;

/**
 * @author liguang
 * @version 1.0.0
 * @Project jeecg-boot
 * @Package com.caocaog.market.util
 * @ClassName a.java
 * @createTime 2022年04月30日 17:54:00
 * @Description TODO
 */
import cn.hutool.crypto.symmetric.AES;
import lombok.extern.slf4j.Slf4j;

import java.util.Base64;

/**
 * @ClassName : DecryptUtil
 * @Description : 解密工具类
 */
@Slf4j
public class DecryptUtil {

    /**
     * 解密敏感数据
     * @param encryptedData
     * @param sessionKey
     * @param iv
     * @return
     */
    public static String decrypt(String encryptedData, String sessionKey, String iv) {
        try {
            Base64.Decoder decoder = Base64.getDecoder();
            byte[] sessionKeyBytes = decoder.decode(sessionKey);
            byte[] ivBytes = decoder.decode(iv);
            byte[] encryptedBytes = decoder.decode(encryptedData);

            AES aes = new AES("CBC", "PKCS7Padding", sessionKeyBytes, ivBytes);
            String res = aes.decryptStr(encryptedBytes);
            log.info("res = [{}]", res);
            return res;
        } catch (Exception e) {
            log.error("解密出现错误", e);
            return "0";
        }
    }
}